﻿using CodeReborn.Classes;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeReborn.Classes
{
    public static class Login
    {
        /// <summary>
        /// This function assumes that the username is already filled in and just enters the password.
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static void LoginToCharacter(string username, string password)
        {

            var currentProcess = Process.GetProcessesByName("ffxivlauncher").ToList().FirstOrDefault();

            if (currentProcess == null)
            {
                Process.Start("F:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\boot\\ffxivboot.exe");
                Thread.Sleep(9999);
            }

            while (currentProcess == null)
            {
                currentProcess = Process.GetProcessesByName("ffxivlauncher").ToList().FirstOrDefault();
                Thread.Sleep(2000);
            }

            if (currentProcess != null)
            {
                IntPtr window = WinAPI.FindWindow("FFXIVLauncher", null);
                WinAPI.SetForegroundWindow(window);
                SendKeys.SendWait(password);
                SendKeys.SendWait("{ENTER}");
                Thread.Sleep(5000);
                SendKeys.SendWait("{ENTER}");
            }

            currentProcess = null;

            currentProcess = Process.GetProcessesByName("ffxiv").ToList().FirstOrDefault();
            while (currentProcess == null)
            {
                currentProcess = Process.GetProcessesByName("ffxiv").ToList().FirstOrDefault();
                Thread.Sleep(2000);
            }

            if (currentProcess != null)
            {
                IntPtr window = WinAPI.FindWindow("FFXIVGAME", null);
                WinAPI.SetForegroundWindow(window);
                WinAPI.ShowWindow(window, WinAPI.SW_MAXIMIZE);
            }

        }
    }
}
